package com.spring.Log;

import com.spring.Entity.AdminEntity;
import com.spring.Entity.ContractEntity;
import com.spring.Entity.UserEntity;
import com.spring.Service.Interface.AdminService;
import com.spring.Service.Interface.ContractService;
import com.spring.Service.Interface.LogService;
import com.spring.Service.Interface.UserServie;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * Created by Arabira on 2016/8/15.
 */
@Aspect
@Component
public class ContractLog {
    @Autowired
    private UserServie userServie;
    @Autowired
    private ContractService contractService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private LogService Log;
    private static String Domian = "合同";

    @Around("execution(* com.spring.Controller.ContractControllerImpl.deleteContractByAdmin(..))")
    public Object delete(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] values = joinPoint.getArgs();

        ContractEntity contractEntity = contractService.findContractById((String) values[0]);
        HttpSession session = (HttpSession) values[1];
        HttpServletRequest request = (HttpServletRequest) values[2];
        String userId = (String) session.getAttribute("SID");
        Map<String, Object> response = (Map<String, Object>) joinPoint.proceed();
        AdminEntity adminEntity = adminService.findById(userId);
        if (response.get("status").equals("ok")) {
            Log.log(adminEntity.getAdminName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "删除合同操作,合同号："
                    + contractEntity.getId() + ", 合同名：" + contractEntity.getContractName());

        } else {
            Log.log(adminEntity.getAdminName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "删除失败，原因是："
                    + response.get("description"));
        }
        return response;
    }

    @Around("execution(* com.spring.Controller.ContractControllerImpl.*Contract(..))")
    public Object add(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] values = joinPoint.getArgs();
        ContractEntity contractEntity = (ContractEntity) values[0];
        HttpSession session = (HttpSession) values[1];
        HttpServletRequest request = (HttpServletRequest) values[2];
        String userId = (String) session.getAttribute("SID");
        UserEntity userEntity = userServie.findById(userId);
        Map<String, Object> response = (Map<String, Object>) joinPoint.proceed();
        if(userEntity == null){
            AdminEntity adminEntity = adminService.findById(userId);
            if (response.get("status").equals("ok")) {
                Log.log(adminEntity.getAdminName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "添加或修改合同操作,合同号："
                        + contractEntity.getId() + ", 合同名：" + contractEntity.getContractName());
            }
            else {
                Log.log(adminEntity.getAdminName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "添加失败，原因是："
                        + response.get("description"));
            }
        }else {
            if (response.get("status").equals("ok")) {
                Log.log(userEntity.getUserName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "添加或修改合同操作,合同号："
                        + contractEntity.getId() + ", 合同名：" + contractEntity.getContractName());
            }
            else {
                Log.log(userEntity.getUserName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "添加失败，原因是："
                        + response.get("description"));
            }
        }
        return response;
    }

    //TODO:表格未提供，无法完成
    @Around("execution(* com.spring.Controller.ContractControllerImpl.addContractExcel(..))")
    public void addExcel(ProceedingJoinPoint joinPoint) {
    }

    @Around("execution(* com.spring.Controller.ContractControllerImpl.upOrDown(..))")
    public Object onOrOff(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] values = joinPoint.getArgs();
        Map response = (Map) joinPoint.proceed();
        HttpSession session = (HttpSession) values[1];
        HttpServletRequest request = (HttpServletRequest) values[2];
        String userId = (String) session.getAttribute("SID");
        UserEntity userEntity = userServie.findById(userId);
        if (response.get("status").equals("ok")) {
            ContractEntity contractEntity = contractService.findContractById((String) values[0]);
            Log.log(userEntity.getUserName(), userId, request.getRemoteAddr(), LocalDateTime.now(),
                    Domian, "修改提醒操作， 合同号：" + contractEntity.getId() + "， 合同名：" + contractEntity.getContractName());


        } else {
            Log.log(userEntity.getUserName(), userId, request.getRemoteAddr(), LocalDateTime.now(),
                    Domian, "修改提醒操作失败，原因是：" + response.get("description"));
        }
        return response;
    }

    @Before("execution(* com.spring.Controller.ContractControllerImpl.findContractByUserName(..))")
    public void findByUserName(JoinPoint joinPoint) {
        Object[] values = joinPoint.getArgs();
        HttpSession session = (HttpSession) values[0];
        HttpServletRequest request = (HttpServletRequest) values[values.length - 1];
        String userId = (String) session.getAttribute("SID");
        UserEntity userEntity = userServie.findById(userId);
        Log.log(userEntity.getUserName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "查询本用户名下合同操作");
        if(userEntity == null){
            AdminEntity adminEntity = adminService.findById(userId);
            Log.log(adminEntity.getAdminName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "查询本用户名下合同操作");
        }else {
            Log.log(userEntity.getUserName(), userId, request.getRemoteAddr(), LocalDateTime.now(), Domian, "查询本用户名下合同操作");
        }
    }

    @Before("execution(* com.spring.Controller.ContractControllerImpl.findContractById(..))")
    public void findBuId(JoinPoint joinPoint) {
        Object[] values = joinPoint.getArgs();
        ContractEntity contractEntity = contractService.findContractById((String) values[0]);
        HttpSession session = (HttpSession) values[1];
        HttpServletRequest request = (HttpServletRequest) values[2];
        String userId = (String) session.getAttribute("SID");
        UserEntity userEntity = userServie.findById(userId);
        if(userEntity == null){
            AdminEntity adminEntity = adminService.findById(userId);
            Log.log(adminEntity.getAdminName(), userId, request.getRemoteAddr(), LocalDateTime.now(),
                    Domian, "查看合同操作， 合同号：" + contractEntity.getContractId() + "， 合同名：" + contractEntity.getContractName());
        }else {
            Log.log(userEntity.getUserName(), userId, request.getRemoteAddr(), LocalDateTime.now(),
                    Domian, "查看合同操作， 合同号：" + contractEntity.getContractId() + "， 合同名：" + contractEntity.getContractName());
        }
    }

}
